﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoPro.Configuration;

namespace CoPro.Provisioning
{
    internal class ContentProviderMap
    {
        /// <summary>
        /// this dictionary contains all providers. The key is the provider name.
        /// </summary>
        private Dictionary<string, IContentProvider> contentProviderDict;

        private string defaultProviderName;

        public ContentProviderMap(Configuration.ContentProviderConfigurationCollection _providerConfigCollection, string _defaultProvider)
        {
            //set defaultProvider           
            defaultProviderName = _defaultProvider;

            //check if default provider is set, if not take the first in the providers list. 
            if (defaultProviderName == null)
                defaultProviderName = _providerConfigCollection[0].Name;

            contentProviderDict = new Dictionary<string, IContentProvider>();

            foreach (ContentProviderConfiguration currentProvider in _providerConfigCollection)
            {
                IContentProvider provider = (IContentProvider)Activator.CreateInstance(Type.GetType(currentProvider.Type));
                provider.Initialize(currentProvider.InitializationSetting);

                contentProviderDict.Add(currentProvider.Name, provider);

            }
        }
        internal IContentProvider this[String _key]
        {
            get
            {
                //if the key is not set, set the defaultProvider as key
                if (_key == null)
                    _key = defaultProviderName;

                return contentProviderDict[_key];
            }
            set
            {

            }
        }
    }
}
